﻿// <copyright file="PermissionTreeHelper.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace XinjeAuthService.Function
{
    using XinjeMesProgram.Entities.Permission;
    using XinjeMesProgram.Entities.RoleAndUser;


    /// <summary>
    /// 权限结构功能.
    /// </summary>
    public static class PermissionTreeHelper
    {
        /// <summary>
        /// 遍历填充权限页面树结构.
        /// </summary>
        /// <param name="menus">菜单集合.</param>
        /// <param name="permissions">权限集合.</param>
        /// <param name="parentGuid">父级菜单GUID.</param>
        /// <param name="isSuperAdministrator">是否为超级管理员角色.</param>
        /// <returns>权限页面树结构.</returns>
        public static List<PermissionMenuTree> FillRecursive(this List<PermissionMenuTree> menus, List<PermissionWithAssignProperty> permissions, string parentGuid, bool isSuperAdministrator = false)
        {
            List<PermissionMenuTree> recursiveObjects = new();
            foreach (var item in menus.Where(x => x.ParentGuid == parentGuid))
            {
                var branch = new PermissionMenuTree
                {
                    AllAssigned = isSuperAdministrator || (permissions.Where(x => x.MenuGuid == item.Guid).Count(x => x.IsAssigned == 0) == 0),
                    Expand = true,
                    Guid = item.Guid,
                    ParentGuid = item.ParentGuid,
                    Permissions = permissions.Where(x => x.MenuGuid == item.Guid).Select(x => new PermissionElement
                    {
                        Name = x.Name,
                        Code = x.Code,
                        IsAssignedToRole = IsAssigned(x.IsAssigned, isSuperAdministrator),
                    }).ToList(),
                    Title = item.Title,
                    Children = FillRecursive(menus, permissions, item.Guid),
                };
                recursiveObjects.Add(branch);
            }

            return recursiveObjects;
        }

        /// <summary>
        /// 过滤授权界面.
        /// </summary>
        /// <param name="menus">界面列表.</param>
        /// <param name="permissions">权限列表.</param>
        /// <param name="parentGuid">父id.</param>
        /// <param name="newMenuList">新的授权页面.</param>
        /// <returns>是否授权.</returns>
        public static bool FilterMenuList(this List<PermissionMenuTree> menus, List<PermissionWithAssignProperty> permissions, string parentGuid, ref List<PermissionMenuTree> newMenuList)
        {
            bool hasAssigned = false;
            foreach (var item in menus.Where(x => x.ParentGuid == parentGuid))
            {
                bool tempAssigned = permissions.Where(x => x.MenuGuid == item.Guid).Any(x => x.IsAssigned == 1) ? true : false;

                // 子界面过滤
                if (FilterMenuList(menus, permissions, item.Guid, ref newMenuList) || tempAssigned)
                {
                    newMenuList.Add(item);
                    hasAssigned = true;
                }
            }

            return hasAssigned;
        }

        /// <summary>
        /// 判断是否授权.
        /// </summary>
        /// <param name="isAssigned">授权情况.</param>
        /// <param name="isSuperAdministrator">是否为管理员.</param>
        /// <returns>授权判断情况.</returns>
        private static bool IsAssigned(int isAssigned, bool isSuperAdministrator)
        {
            if (isSuperAdministrator)
            {
                return true;
            }

            return isAssigned == 1;
        }
    }
}
